package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.common.R;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        //获取用户id
       log.info("userId:{}", ThreadLocalUtil.getCurrentId());
       //log.info("useId:{}",request.getSession().getAttribute("user"));
        Long userId= ThreadLocalUtil.getCurrentId();
         shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //根据dishId判断是套餐还是菜品或者setmealId
        if(dishId!=null){//菜品
           lqw.eq(ShoppingCart::getDishId,dishId);
        }else {//套餐
           lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if(one!=null){
            Integer number = one.getNumber();
            one.setNumber(++number);
            shoppingCartService.updateById(one);
        }else {
           shoppingCart.setNumber(1);
           shoppingCart.setUserId(userId);
           shoppingCartService.save(shoppingCart);
        }
        return R.success("添加购物车成功");
    }

    /**
     * 取消购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R updateCart(@RequestBody ShoppingCart shoppingCart){
        Long userId = ThreadLocalUtil.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if(one!=null){
            Integer number = one.getNumber();
            number--;
            one.setNumber(number);
            if(number>0){
                shoppingCartService.updateById(one);
            }else {
                shoppingCartService.removeById(one.getId());
            }
        }
        return R.success("修改成功");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
     public R clearCart(){
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,ThreadLocalUtil.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
     }

    /**
     * 购物车列表
     * @return
     */
    @GetMapping("/list")
    public R cartList(){
        //根据userid进行查询
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,ThreadLocalUtil.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return R.success(shoppingCartList);
    }
}
